package com.garden.minioclient.service;

import com.garden.minioclient.config.MinioConfig;
import com.garden.minioclient.utils.DownLoadUtil;
import com.garden.minioclient.vo.FileObject;
import com.google.common.io.ByteStreams;
import io.minio.*;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * @author Garden
 * @date: 2023/10/24 10:47
 */
@Slf4j
@Service
public class MinioService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioConfig minioConfig;


    /**
     * 查看bucket是否存在
     *
     * @param bucketName
     * @return
     */
    public boolean bucketExists(String bucketName) {
        try {
            boolean result = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(bucketName)
                    .build());
            return result;
        } catch (Exception e) {
            log.error("bucketExists error, msg: {}", e.getMessage());
            return false;
        }
    }

    /**
     * 上传文件
     *
     * @param multipartFile
     * @return
     */
    public boolean uploadFile(MultipartFile multipartFile) {
        try {
            InputStream inputStream = multipartFile.getInputStream();
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(multipartFile.getOriginalFilename())
                    .stream(inputStream, multipartFile.getSize(), -1)
                    .contentType(multipartFile.getContentType())
                    .build());
            return true;
        } catch (Exception e) {
            log.error("uploadFile error, msg: {}", e.getMessage());
            return false;
        }
    }


    /**
     * 下载文件
     *
     * @param fileName
     * @param request
     * @param response
     * @return
     */
    public void downloadFile(String fileName, HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setCharacterEncoding("UTF-8");
            ServletOutputStream os = response.getOutputStream();
            GetObjectResponse is = minioClient.getObject(GetObjectArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(fileName)
                    .build());
            response.setHeader("Content-Disposition", "attachment;filename=" + DownLoadUtil.getFileName(request.getHeader("user-agent"), fileName));
            ByteStreams.copy(is, os);
            os.flush();
        } catch (Exception e) {
            log.error("downloadFile error, msg: {}", e.getMessage());
        }

    }

    /**
     * 删除文件
     *
     * @param fileName
     * @return
     */
    public boolean removeFile(String fileName) {
        try {
            minioClient.removeObject(RemoveObjectArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(fileName)
                    .build());
            return true;
        } catch (Exception e) {
            log.error("removeFile error, msg: {}", e.getMessage());
            return false;
        }
    }

    /**
     * 获取文件Url
     *
     * @param fileName
     * @return
     */
    public String getFileUrl(String fileName) {
        try {
            return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(fileName)
                    .method(Method.GET)
                    .build());
        } catch (Exception e) {
            log.error("getFileUrl error, msg: {}", e.getMessage());
            return "";
        }
    }

    /**
     * 查看文件对象
     *
     * @param fileName
     * @return
     */
    public FileObject getFileObject(String fileName) {
        FileObject fileObject = new FileObject();
        try {
            GetObjectResponse result = minioClient.getObject(GetObjectArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(fileName)
                    .build());
            fileObject.setBucket(result.bucket());
            fileObject.setRegion(result.region());
            fileObject.setObject(result.object());
        } catch (Exception e) {
            log.error("getFileObject error, msg: {}", e.getMessage());
        }
        return fileObject;
    }

}
